package com.ryskoo.framework.core.repository;

import com.ryskoo.framework.core.bean.BasicPO;
import com.ryskoo.framework.core.bean.QueryMessageDTO;
import com.ryskoo.framework.core.utils.RedisUtil;
import com.ryskoo.framework.core.utils.ReflectHelper;

import java.util.List;

public class BasicRedisImpl<M extends BasicPO> implements IBasicRepository<M> {
	private final String KEY_PRE;
	private RedisUtil redisClient;
	private IBasicRepository reoisitory = null;

	public void setRedisClient(RedisUtil redisClient) {
		this.redisClient = redisClient;
	}

	public void setDao(IBasicRepository reoisitory) {
		this.reoisitory = reoisitory;
	}

	public BasicRedisImpl(String key_pre) {
		this.KEY_PRE = key_pre;
	}

	@Override
	public void create(M m) {
		this.reoisitory.create(m);
		redisClient.setObject(this.KEY_PRE + m.getUuid(), m);
	}

	@Override
	public void update(M m) {
        this.reoisitory.update(m);
        redisClient.setObject(this.KEY_PRE + m.getUuid(), m);
	}

	@Override
	public void createOrUpdate(M m) {
		this.reoisitory.createOrUpdate(m);
        redisClient.setObject(this.KEY_PRE + m.getUuid(), m);
	}

	@Override
	public void delete(M m) {
        this.reoisitory.update(m);
        redisClient.remove(this.KEY_PRE + m.getUuid());
	}

	@Override
	public void deletes(List<String> needDeleteUuids) {
        this.reoisitory.deletes(needDeleteUuids);
        for (String deleteUuid : needDeleteUuids) {
            redisClient.remove(this.KEY_PRE + deleteUuid);
        }
	}

	@Override
	public M getByUuid(String uuid) {
	    Object obj = this.redisClient.getObject(this.KEY_PRE + uuid, ReflectHelper.getModelClass(this.getClass()));
        if (null != obj) {
            return (M) obj;
        }
        M m = (M) this.reoisitory.getByUuid(uuid);
        if (null == m) {
        	return null;
        }
        redisClient.setObject(this.KEY_PRE + m.getUuid(), m);
        return m;
	}

	/**
	 * 不带条件查询所有对象
	 */
	@Override
	public List<M> getAll() {
		return reoisitory.getAll();
	}
	
	
	/**
	 * 不带条件查询所有Id
	 */
	@Override
	public List<String> getAllUuids() {
		return reoisitory.getAllUuids();
	}


	@Override
	public List<M> queryByCondition(QueryMessageDTO qm, boolean byPage) {
		return reoisitory.queryByCondition(qm, byPage);
	}

	@Override
	public List<M> queryByNativeSql(QueryMessageDTO qm, boolean byPage) throws NoSuchFieldException {
		return reoisitory.queryByNativeSql(qm, byPage);
	}

	@Override
	public List<String> getUuidsByCondition(QueryMessageDTO qm, boolean byPage) {
		return reoisitory.getUuidsByCondition(qm, byPage);
	}

	@Override
	public int getCount(QueryMessageDTO qm) {
		return reoisitory.getCount(qm);
	}

	@Override
	public int getCountByNativeSql(QueryMessageDTO qm) throws NoSuchFieldException {
		return reoisitory.getCountByNativeSql(qm);
	}

	@Override
	public void batchCreate(List<M> paramList) {
		reoisitory.batchCreate(paramList);
	}

	@Override
	public void batchUpdate(List<M> paramList) {
		reoisitory.batchUpdate(paramList);
	}

	
	/**
	 * 游离态更新/新增实体
	 * 
	 * @param m
	 */
	@Override
	public void merge(M m) {
		reoisitory.merge(m);
		redisClient.setObject(this.KEY_PRE + m.getUuid(), m);
	}
}
